from ely.poundmonitor.parse import parse_log
from ely.poundmonitor.parse import monitor
import time
from ely.poundmonitor.stats import Stats
from zc.ngi import END_OF_DATA
from zc.ngi.async import listener
from ely.poundmonitor.tailfollow.tailstream import TailStream


def main(args):

    print args
    verb = args[0]
    if verb == 'monitor':
        # Monitor log file watching for updates.
        port = int(args[1])
        log = args[2]
        stats = Stats()
        def connect(connection):
            stats.dump(connection)
            connection.write(END_OF_DATA)
        listener(('127.0.0.1', port), connect)
        stream = TailStream(log)
        monitor(stream, stats)

    elif verb == 'process':
        # Process whole log file.
        log = open(args[1])
        backend = args[2]
        parse_log(log, backend)

